package com.fr.data.impl;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.data.TableDataException;
import com.fr.data.core.DataUtils;
import com.fr.data.core.db.ColumnInformation;
import com.fr.data.core.db.DBUtils;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dialect.DialectFactory;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class LayerAdapter extends TableDataAdapter
{
  private static final long serialVersionUID = 1L;
  private transient int currentRowIndex = -1;
  private transient Object[] currentRowValue = null;
  private String rowCountQuery;

  public LayerAdapter(DatabaseConnection paramDatabaseConnection, String paramString)
  {
    super(paramDatabaseConnection, paramString, 0);
  }

  public int getRowCount()
    throws TableDataException
  {
    return shortcutGetRowCount();
  }

  protected int shortcutGetRowCount()
  {
    if (this.shortcutRowCount == -1)
    {
      long l1 = System.currentTimeMillis();
      String str = this.rowCountQuery;
      if ((str == null) && (DBUtils.isProcedure(this.sql)))
      {
        this.shortcutRowCount = -2147483648;
        FRContext.getLogger().log(Level.WARNING, Inter.getLocText("DBTableData_Not_Get_RowCount"));
        throw new RuntimeException(Inter.getLocText("DBTableData_Not_Get_RowCount"));
      }
      Connection localConnection = null;
      try
      {
        localConnection = this.database.createConnection();
        if (this.dialect == null)
          this.dialect = DialectFactory.generateDialect(localConnection);
        if (str == null)
          str = this.dialect.getCountSql(this.sql);
        FRContext.getLogger().log(Level.INFO, "RowCount SQL:\n" + str);
        Statement localStatement = localConnection.createStatement();
        ResultSet localResultSet = localStatement.executeQuery(str);
        localResultSet.next();
        this.shortcutRowCount = localResultSet.getInt(1);
        localResultSet.close();
        localStatement.close();
      }
      catch (Exception localException)
      {
        this.shortcutRowCount = -2147483648;
        throw new RuntimeException(localException.getMessage());
      }
      finally
      {
        if (localConnection != null)
          try
          {
            localConnection.close();
          }
          catch (SQLException localSQLException)
          {
            FRContext.getLogger().log(Level.WARNING, localSQLException.getMessage(), localSQLException);
          }
      }
      long l2 = System.currentTimeMillis();
      FRContext.getLogger().log(Level.INFO, "RowCount costs " + ((l2 - l1) / 1000L) + " s.");
    }
    return this.shortcutRowCount;
  }

  public Object getValueAt(int paramInt1, int paramInt2)
    throws TableDataException
  {
    return ((hasRow(paramInt1)) ? this.currentRowValue[paramInt2] : null);
  }

  protected boolean hasRow(int paramInt)
    throws TableDataException
  {
    if (paramInt < 0)
      return false;
    if (paramInt == this.currentRowIndex)
      return true;
    if (paramInt < this.currentRowIndex)
      dealWithException(new Exception("Error Index!"), paramInt);
    else if (this.isCheckRowOver)
      return false;
    initConnectionAndResultAndCheckInColumns();
    if (this.connection == null)
      return false;
    Dialect localDialect = DialectFactory.generateDialect(this.connection);
    int i = getColumnCount();
    int j = 0;
    try
    {
      String str1 = this.database.getOriginalCharsetName();
      String str2 = this.database.getNewCharsetName();
      do
      {
        if (!(this.resultSet.next()))
          break label229;
        this.currentRowIndex += 1;
      }
      while (this.currentRowIndex < paramInt);
      this.currentRowValue = new Object[i];
      for (int k = 0; k < i; ++k)
        try
        {
          this.currentRowValue[k] = DataUtils.getResultSetObject(this.resultSet, this.columnInformations[k].getColumnType(), k + 1, str1, str2, localDialect);
        }
        catch (SQLException localSQLException)
        {
          FRContext.getLogger().log(Level.WARNING, localSQLException.getMessage(), localSQLException);
        }
      if (this.currentRowIndex == this.shortcutRowCount - 1)
        j = 1;
      else
        return true;
      if (j == 0)
        label229: this.shortcutRowCount = (this.currentRowIndex + 1);
      this.isCheckRowOver = true;
      releaseConnection();
    }
    catch (Exception localException)
    {
      dealWithException(localException, paramInt);
    }
    return j;
  }

  private void dealWithException(Exception paramException, int paramInt)
    throws TableDataException
  {
    try
    {
      releaseConnection();
    }
    catch (Exception localException)
    {
    }
    this.isCheckRowOver = true;
    throw new TableDataException(paramException.getMessage() + "\nQuery is:" + this.sql + "\nrowIndex to get is:" + paramInt, paramException);
  }

  protected void initRowValueStatus()
  {
    this.currentRowIndex = -1;
    this.currentRowValue = null;
  }
}